import axios from 'axios';

const $ = axios.create();

$.post = (url, params = {}) => {
  let axiosOption = {
    url,
    transformRequest: [
      function (data) {
        data = JSON.stringify(data);
        return data;
      },
    ],
    method: 'post',
    headers: {
      'Content-Type': 'application/json',
    },
  };
  axiosOption.data = params;
  return new Promise((resolve, reject) => {
    return axios(axiosOption)
      .then((res) => {
        resolve(res.data);
      })
      .catch(reject);
  });
};

$.get = (url, params = {}) => {
  let axiosOption = {
    url,
    transformRequest: [
      function (data) {
        data = JSON.stringify(data);
        return data;
      },
    ],
    method: 'get',
    headers: {
      'Content-Type': 'application/json',
    },
  };
  axiosOption.data = params;
  return new Promise((resolve, reject) => {
    return axios(axiosOption)
      .then((res) => {
        resolve(res.data);
      })
      .catch(reject);
  });
};

export default $;
